﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Specialized;
using LHS.Core.TableInfo;

namespace LHS.Win
{
    public partial class frmHinhAnh : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmHinhAnh(string clrISN, bool _Insert)
        {
            this.InitializeComponent();
            this._clrISN = clrISN;
            Insert = _Insert;
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPEG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp";
            dialog.FilterIndex = 1;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.path = dialog.FileName.ToString();
                this.ext = Path.GetExtension(this.path);
                this.lbPath.Text = this.path;
                this.picPatients.Image = Image.FromFile(this.path);
                this.picPatients.SizeMode = PictureBoxSizeMode.Normal;
                this.picPatients.ClientSize = new Size(700, 550);
            }
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            if (this.picPatients.Image != null)
            {
                PrintDocument document = new PrintDocument();
                document.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                document.Print();
            }
            else
            {
                MessageBox.Show("Vui lòng chọn ảnh", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            string str = this._clrISN;
            if (this.tbMoTa.Text == "")
            {
                MessageBox.Show("Vui lòng nhập mô tả", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else if (File.Exists(this.path))
            {
                object iIdentity = objController.GetIDENT_CURRENT(InfoClinicalPicture.TABLENAME);
                if (iIdentity is Int64)
                {
                    string destFileName = Application.StartupPath + @"\PatientImages\" + Convert.ToString(Convert.ToInt64(iIdentity.ToString()) + 1) + this.ext;
                    File.Copy(this.path, destFileName, true);
                    try
                    {
                        ListDictionary param = new ListDictionary();
                        param.Add(InfoClinicalRecord.CLRISN, str);
                        param.Add(InfoScanPicture.SCPPICTURE, this.tbMoTa.Text.ToString());
                        param.Add(InfoScanPicture.SCPNOTE, destFileName);
                        objController.Exec_StoreProc("sp_ScanPicture_Insert", param);

                        MessageBox.Show("Ảnh đã được lưu", "Chèn Hình", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.tbMoTa.Text = "";
                    }
                    catch (Exception ex)
                    {
                        Class.ShowException(ex);
                    }
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn ảnh!", "Chưa chọn ảnh", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(this.picPatients.Image, e.Graphics.VisibleClipBounds);
            e.HasMorePages = false;
        }

    }
}